package com.smallA.controller;

import com.smallA.constant.DataConstant;
import com.smallA.po.Exam;
import com.smallA.po.Grade;
import com.smallA.po.TopicType;
import com.smallA.service.*;
import com.smallA.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author leon
 * @create 2021/8/14 12:24
 */
@Controller
public class TeacherController {
    @Resource
    private UserService userService;
    @Resource
    private GradeService gradeService;
    @Resource
    private ExamService examService;
    @Resource
    private ExamQuestionService examQuestionService;
    @Resource
    private TestService testService;
    @Resource
    private QuestionService questionService;

    @RequestMapping("/Teacher/exam")
    @ResponseBody
//    @Secured(AccessConstant.TEACHER_ACCESS)
    public Object Exam(int id) {
       /* Map<String, Object> map = examQuestionService.selectExamById(id);
        ResultExam exam = new ResultExam();


        //exam.setQuestion(map);*/
        //这里我是重新写了个exam的查询,所以顺手把你这里换了
        return examService.selectExamByExamId(id);
    }


    /**
     * 获取班级列表
     */
    @RequestMapping("/Teacher/class")
//    @Secured(AccessConstant.TEACHER_ACCESS)
    @ResponseBody
    public Object classroom(int tid){
        return userService.selectClassById(tid);
    }

    /**
     * 班级考试情况
     */
    @RequestMapping("/Teacher/classTest")
    @ResponseBody
//    @Secured(AccessConstant.TEACHER_ACCESS)
    public Object classTest(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put(DataConstant.ALL_TEST, userService.selectTestByClassId(id));
        map.put(DataConstant.CLASSROOM, userService.selectClassByClassId(id));
        return map;
    }

    /**
     * 班级考试成绩情况
     */
    @ResponseBody
    @RequestMapping("/Teacher/classGrade")
//    @Secured(AccessConstant.TEACHER_ACCESS)
    public Object classGrade(int tid, int cid){
        Map<String,Object> map = new HashMap<>();
        map.put(DataConstant.GRADE_LIST,gradeService.selectGradeByTestId(tid));
        map.put(DataConstant.STUDENT_LIST,userService.selectStuByClassId(cid));
        return map;
    }

    @ResponseBody
    @RequestMapping("/Teacher/getExam")
//    @Secured(AccessConstant.TEACHER_ACCESS)
    public Result getExam(int gid) {
        return examService.selectExamById(gid);
    }

    @ResponseBody
    @RequestMapping("/Teacher/autoExam")
    public Object autoTest() {
        List<TopicType> topicTypes = testService.selectAllTopicTpe();
        Map<String,Object> map = new HashMap<>();
        map.put(DataConstant.TOPIC_TYPE_DATA, topicTypes);
        map.put("Question",examQuestionService.selectAllQuestion());
        return map;
    }

    @ResponseBody
    @RequestMapping("/Teacher/autoAdd")
    public Object autoAdd(Integer[] topicTypeIds, Integer choiceCount, Integer fillCount, Integer shortCount, String analyze, int score,int tid) {
        Exam exam = examService.autoAdd(topicTypeIds,choiceCount, fillCount,shortCount,analyze,score,tid);
        return exam;
    }

//    @Secured(AccessConstant.TEACHER_ACCESS)
@RequestMapping("/Teacher/submit")
    public String submit(Model model){
        String time = "10";
        model.addAttribute("time",time);
        return "teacher/submit";
    }

    @ResponseBody
    @RequestMapping("/Teacher/endTime")
    public Long getEndTime(Integer gid){
        Grade grade = gradeService.selectGradeById(gid);
        long i = grade.getTest().getTestTime()* 60000L+ grade.getTest().getBeginDate().getTime()-System.currentTimeMillis();
        System.out.println(i);
        return i;
    }

    @ResponseBody
    @RequestMapping("/Teacher/uploadFile")
    public Object upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        String path = request.getServletContext().getRealPath("/upload");
        return questionService.insertQuestion(path, file);
    }
}
